package wiki.douzi.helpyougrow.service.impl;

import wiki.douzi.helpyougrow.domain.User;
import wiki.douzi.helpyougrow.mapper.UserMapper;
import wiki.douzi.helpyougrow.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import javax.annotation.Resource;

/**
 * (User)表服务实现类
 *
 * @author Douzi
 * @since 2022-06-18 16:39:26
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */
    @Override
    public List<User> queryAll() {
        return this.userMapper.queryAll();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userMapper.queryById(id);
    }

    @Override
    public User queryByName(String username) {
            return this.userMapper.queryByName(username);
    }

    @Override
    public User login(String username,String password) {
        User user = this.userMapper.queryByName(username);
        if (user != null) {
            if (user.getPassword().equals(password)){
                return user;
            }
        }
        return null;
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userMapper.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userMapper.deleteById(id) > 0;
    }
}

